<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minimumAverageDifference = function (nums) {
      let ans = 0;
      let avarage = Infinity;
      const n = nums.length;
      let sum = nums.reduce((total, curr) => total + curr, 0);
      let currSum = 0;

      for (let i = 0; i < n; i++) {
        currSum += nums[i];
        sum -= nums[i];
        const currAvarage = Math.abs(Math.floor(currSum / (i + 1)) - Math.floor(n - i - 1 === 0 ? 0 : sum / (n - i - 1)));
        if (currAvarage < avarage) {
          ans = i;
          avarage = currAvarage;
        }
      }

      return ans;
    };
    console.log(minimumAverageDifference([2,5,3,9,5,3]));
  </script>
</body>

</html>